package com.example.myview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.example.bean.Mystyle;
import com.example.bean.Point;
import com.example.sharedPref.SharePrefConstant;
import com.example.sharedPref.SharedPref;
import com.example.utils.DrawStyleUtil;
import com.example.utils.NumberUtil2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StyleView {
    public static SharedPref mSharedPref = null;
    private   int color=0xff4385F3;
    private   int width=2;
    /**
     * 0 画实线  大于0画虚线
     */
    private   int xuxian_width=0;


    public  final  static int STYLE_TWO_POINT=1;
    public  final  static int STYLE_THREE_POINT=2;
    public  final  static int STYLE_HENGLINE=3;
    public  final  static int STYLE_SHULINE=4;
    public  final  static int STYLE_RECT=5;
    //当前正在画的样式

    Mystyle  mystyle=new Mystyle();
    /**
     * 储存所有的样式
     */
    ArrayList<Mystyle> lists=new ArrayList<>();
    //样式回调
    MsgView.IcbCall icb;
    /**
     * 当前点
     */
    private Point cpoint;

    public int styleType=-1;
    /**
     * 0没有开始画 1没有画到最后一个点 2画到最后1个点 3移动样式时的状态
     */
    public int state=0;
    KLineView kLineView;
    public StyleView(Context context)
    {
        mSharedPref = SharedPref.getInstance("kl", context);
        color=mSharedPref.getSharePrefInteger(SharePrefConstant.color,0xff4385F3);
        width=mSharedPref.getSharePrefInteger(SharePrefConstant.line_width,2);
        xuxian_width=mSharedPref.getSharePrefInteger(SharePrefConstant.xuxian_width,0);
    }
    public  void setkLineView(KLineView kLineView)
    {
        this.kLineView=kLineView;
    }

    public void setBack(MsgView.IcbCall icb)
    {
        this.icb=icb;
    }

    DrawStyleUtil du=new DrawStyleUtil();
    public void setState(int state)
    {
        this.state=state;


    }
    public int getState()
    {

        return state;
    }
    public  void setStyle(int type)
    {
        this.styleType=type;
        mystyle=new Mystyle();
        mystyle.index=type;
        state=1;

        mystyle.setColor(color);
        mystyle.setLine_width(width);
        mystyle.setXuxian_width(xuxian_width);
    }
    public void drawPoint(Canvas canvas)
    {
        if(state==0)
        {
            return;
        }
      if(mystyle.list==null||mystyle.list.size()<=0)
      {
          return;
      }
      if(styleType==STYLE_TWO_POINT)
      {
          if(mystyle.list.size()==1)
          {
              du.drawPoint(canvas,mystyle.list.get(0),mystyle);
          }
          else if(mystyle.list.size()==2)
          {
              du.drawPoint(canvas,mystyle.list.get(0),mystyle);
              du.drawPoint(canvas,mystyle.list.get(1),mystyle);
              du.drawLine(canvas,mystyle.list.get(0),mystyle.list.get(1),mystyle);


              setState(2);

          }
      }
        if(styleType==STYLE_THREE_POINT)
        {
            if(mystyle.list.size()==1)
            {
                du.drawPoint(canvas,mystyle.list.get(0),mystyle);
            }
            else if(mystyle.list.size()==2)
            {
                du.drawPoint(canvas,mystyle.list.get(0),mystyle);
                du.drawPoint(canvas,mystyle.list.get(1),mystyle);
                du.drawLine(canvas,mystyle.list.get(0),mystyle.list.get(1),mystyle);



            }
            else if(mystyle.list.size()==3)
            {
                du.drawPoint(canvas,mystyle.list.get(0),mystyle);
                du.drawPoint(canvas,mystyle.list.get(1),mystyle);
                du.drawPoint(canvas,mystyle.list.get(2),mystyle);
                du.drawLine(canvas,mystyle.list.get(0),mystyle.list.get(1),mystyle);
                du.drawLine(canvas,mystyle.list.get(1),mystyle.list.get(2),mystyle);



                setState(2);

            }
        }
        if(styleType==STYLE_RECT)
        {
            if(mystyle.list.size()==1)
            {
                du.drawPoint(canvas,mystyle.list.get(0),mystyle);
            }
            else if(mystyle.list.size()==4)
            {
                du.drawPoint(canvas,mystyle.list.get(0),mystyle);
                du.drawPoint(canvas,mystyle.list.get(1),mystyle);
                du.drawPoint(canvas,mystyle.list.get(2),mystyle);
                du.drawPoint(canvas,mystyle.list.get(3),mystyle);
                du.drawLine(canvas,mystyle.list.get(0),mystyle.list.get(1),mystyle);
                du.drawLine(canvas,mystyle.list.get(1),mystyle.list.get(2),mystyle);
                du.drawLine(canvas,mystyle.list.get(2),mystyle.list.get(3),mystyle);
                du.drawLine(canvas,mystyle.list.get(3),mystyle.list.get(0),mystyle);


                setState(2);

            }
        }
      else if(styleType==STYLE_HENGLINE)
      {
          float x=mystyle.list.get(1).getX();
          Point point=new Point();
          point.setX(x/2);
          point.setY(mystyle.list.get(0).getY());
          du.drawPoint(canvas,point,mystyle);
          du.drawLine(canvas,mystyle.list.get(0),mystyle.list.get(1),mystyle);
          String price= NumberUtil2.getString("1",mystyle.list.get(1).getPrice()+"",kLineView.getScalePrice());

          du.drawPriceTextWithRect(canvas,0,mystyle.list.get(0).getY(),kLineView.getHeight(),kLineView.getWidth(),price,mystyle);


          setState(2);

      }
      else if(styleType==STYLE_SHULINE)
      {
          float y=mystyle.list.get(1).getY();
          Point point=new Point();
          point.setX(mystyle.list.get(0).getX());
          point.setY(y/2);
          du.drawPoint(canvas,point,mystyle);
          du.drawLine(canvas,mystyle.list.get(0),mystyle.list.get(1),mystyle);

          du.drawTimeTextWithRect(canvas,mystyle.list.get(0).getX(),kLineView.mainH,kLineView.getWidth(),mystyle.list.get(0).getTime(),mystyle);


          setState(2);

      }

      //icb.call(2,0,0);

    }
    public void addPoint(Point point)
    {
        if(styleType==STYLE_HENGLINE)
        {
            Point point1=new Point();
            point1.setPrice(point.getPrice());
            point1.setTime(point.getTime());
            mystyle.list.add(point1);

            Point point2=new Point();
            point2.setTime(point.getTime());
            point2.setPrice(point.getPrice());
            mystyle.list.add(point2);
            lists.add(mystyle);
            return;
        }
        else  if(styleType==STYLE_SHULINE)
        {
            Point point1=new Point();
            point1.setTime(point.getTime());
            point1.setPrice(point.getPrice());
            point1.setOffsetX(point.getOffsetX());
            mystyle.list.add(point1);

            Point point2=new Point();
            point2.setTime(point.getTime());
            point2.setPrice(point.getPrice());
            point2.setOffsetX(point.getOffsetX());

            mystyle.list.add(point2);
            lists.add(mystyle);
            return;
        }
        else  if(styleType==STYLE_TWO_POINT)
        {
            mystyle.list.add(point);
            if(mystyle.list.size()==2)
            {
                lists.add(mystyle);
            }
        }
        else  if(styleType==STYLE_THREE_POINT)
        {
            mystyle.list.add(point);
            if(mystyle.list.size()==3)
            {
                lists.add(mystyle);
            }
        }
        else if(styleType==STYLE_RECT)
        {
              if(mystyle.list.size()==0)
              {
                  mystyle.list.add(point);
              }
              else if(mystyle.list.size()==1)
              {
                  //判断第2个点在第1个点的左侧还是右侧
                  Point point1=mystyle.list.get(0);
                  if(point.getOffsetX()>=point1.getOffsetX())
                  {
                     //在第1个点的右侧
                      if(point.getPrice()>=point1.getPrice())
                      {
                          //在第1个点的右侧上方
                          Point point2=new Point();
                          point2.setOffsetX(point.getOffsetX());
                          point2.setPrice(point1.getPrice());

                          Point point4=new Point();
                          point4.setOffsetX(point1.getOffsetX());
                          point4.setPrice(point.getPrice());

                          mystyle.list.add(point2);
                          mystyle.list.add(point);
                          mystyle.list.add(point4);
                      }
                      else
                      {
                          //在第1个点的右侧下方
                          Point point2=new Point();
                          point2.setOffsetX(point1.getOffsetX());
                          point2.setPrice(point.getPrice());

                          Point point4=new Point();
                          point4.setOffsetX(point.getOffsetX());
                          point4.setPrice(point1.getPrice());

                          mystyle.list.add(point2);
                          mystyle.list.add(point);
                          mystyle.list.add(point4);
                      }
                  }
                  else
                  {
                      //在第1个点的左侧
                      if(point.getPrice()>=point1.getPrice())
                      {
                          //在第1个点的左侧上方
                          Point point2=new Point();
                          point2.setOffsetX(point1.getOffsetX());
                          point2.setPrice(point.getPrice());

                          Point point4=new Point();
                          point4.setOffsetX(point.getOffsetX());
                          point4.setPrice(point1.getPrice());

                          mystyle.list.add(point2);
                          mystyle.list.add(point);
                          mystyle.list.add(point4);
                      }
                      else
                      {
                          //在第1个点的左侧下方
                          Point point2=new Point();
                          point2.setOffsetX(point.getOffsetX());
                          point2.setPrice(point1.getPrice());

                          Point point4=new Point();
                          point4.setOffsetX(point1.getOffsetX());
                          point4.setPrice(point.getPrice());

                          mystyle.list.add(point2);
                          mystyle.list.add(point);
                          mystyle.list.add(point4);
                      }
                  }




              }
            if(mystyle.list.size()==4)
            {
                lists.add(mystyle);
            }
            return;
        }

    }

    /**
     * 画历史样式
     * @param canvas
     */
    private void drawLiShiStyle(Canvas canvas)
    {
        Mystyle style=null;
        for(int i=0;i<lists.size();i++)
        {
            style=lists.get(i);
            if(style.index==STYLE_TWO_POINT)
            {
                du.drawLine(canvas,style.list.get(0),style.list.get(1),style);
            }
            else if(style.index==STYLE_HENGLINE)
            {
                if(style.list.get(0).getY()>=(kLineView.mainH))
                {

                }
                else {
                    du.drawLine(canvas,style.list.get(0),style.list.get(1),style);
                    String price= NumberUtil2.getString("1",style.list.get(1).getPrice()+"",kLineView.getScalePrice());

                    du.drawPriceTextWithRect(canvas,0,style.list.get(0).getY(),kLineView.getHeight(),kLineView.getWidth(),price,style);
                }



            }
            else if(style.index==STYLE_SHULINE)
            {
                du.drawLine(canvas,style.list.get(0),style.list.get(1),style);
                if(style.list.get(0).getX()>=kLineView.getWidth())
                {
                    return;
                }
                du.drawTimeTextWithRect(canvas,style.list.get(0).getX(),kLineView.mainH,kLineView.getWidth(),style.list.get(0).getTime(),style);
            }
            else if(style.index==STYLE_THREE_POINT)
            {
                du.drawLine(canvas,style.list.get(0),style.list.get(1),style);
                du.drawLine(canvas,style.list.get(1),style.list.get(2),style);
            }
            else if(style.index==STYLE_RECT)
            {

                du.drawLine(canvas,style.list.get(0),style.list.get(1),style);
                du.drawLine(canvas,style.list.get(1),style.list.get(2),style);
                du.drawLine(canvas,style.list.get(2),style.list.get(3),style);
                du.drawLine(canvas,style.list.get(3),style.list.get(0),style);
            }
            else
            {
                du.drawLine(canvas,style.list.get(0),style.list.get(1),style);
            }
        }

    }
    /**
     * 画历史样式
     * @param canvas
     * @param
     */
    public void drawStyle(Canvas canvas)
    {
        if(kLineView==null)
        {
            return;
        }
        countXY();
        drawLiShiStyle(canvas);
        drawPoint(canvas);
    }
    private void countXY()
    {
        Mystyle mystyle=null;
        for(int i=0;i<lists.size();i++)
        {
            mystyle=lists.get(i);
            for(int a=0;a<mystyle.list.size();a++)
            {
                Point point=mystyle.list.get(a);
                point.setX(kLineView.getXZuoBiao(point.getOffsetX()));
                point.setY(kLineView.parseNumber(point.getPrice()));
                if(mystyle.index==STYLE_HENGLINE)
                {
                    if(a==0)
                    {
                        point.setX(0);
                    }
                    else
                    {
                        point.setX(kLineView.getWidth());
                    }
                }
                else  if(mystyle.index==STYLE_SHULINE)
                {
                    if(a==0)
                    {
                        point.setY(0);
                    }
                    else
                    {
                        point.setY(kLineView.mainH);
                    }
                }

            }
        }
        if(this.mystyle!=null) {
            for (int a = 0; a < this.mystyle.list.size(); a++) {
                Point point = this.mystyle.list.get(a);
                point.setX(kLineView.getXZuoBiao(point.getOffsetX()));
                point.setY(kLineView.parseNumber(point.getPrice()));
                if (this.mystyle.index == STYLE_HENGLINE) {
                    if (a == 0) {
                        point.setX(0);
                    } else {
                        point.setX(kLineView.getWidth());
                    }
                } else if (this.mystyle.index == STYLE_SHULINE) {
                    if (a == 0) {
                        point.setY(0);
                    } else {
                        point.setY(kLineView.mainH);
                    }
                }

            }
        }
    }

    /**
     * 得到当前点击的点在
     * @param
     */
    public Mystyle getOnMysyle(float x,float y)
    {
        countXY();

        for(int i=0;i<lists.size();i++)
        {
            Mystyle style=lists.get(i);
            int a=style.isOn(x,y);
           // Log.e("on",a+"");
            if(a>0)
            {

                return style;
            }


        }
        return null;
    }

    /**
     * 设置当前选中的样式
     * @param style
     */
    public void setSelectStyle(Mystyle style)
    {
        mystyle=style;
        styleType=style.index;
        setState(2);


    }

    /**
     * 拖动圆点的时候更新样式
     * @param point
     */
    public void updateSelecStyle(Point point)
    {
        mystyle.list.set(mystyle.getSelect(),point);
        styleType=mystyle.index;
        setState(2);
    }

    /**
     * 移动样式
     * @param x x移动的偏移
     * @param y y移动的偏移
     */
    public void moveSelectStyle(float x,float y)
    {

        for(int i=0;i<mystyle.list.size();i++)
        {
            Point point=mystyle.list.get(i);

        }
    }
    /**
     * 如果当前有选中或者正在画的样式，删除当前样式
     * 否则 删除所有的样式
     */
    public void delete()
    {

        if(state==1||state==2)
        {
            lists.remove(mystyle);
            mystyle=null;
        }
        else
        {
            mystyle=null;
            lists.clear();
        }
        setState(0);
        kLineView.invalidate();

    }

    /**
     * 删除最后一个
     */
    public void invoke()
    {
        if(mystyle!=null)
        {
            int index = lists.indexOf(mystyle);
            if (index == lists.size() - 1) {
                lists.remove(mystyle);
                mystyle = null;
            } else {
                lists.remove(lists.size()-1);
            }
            setState(0);
            kLineView.invalidate();
        }
        else
        {
            if(lists.size()>0)
            {
                lists.remove(lists.size()-1);
                setState(0);
                kLineView.invalidate();
            }

        }
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
        if(mystyle!=null)
        {
           mystyle.setColor(color);
        }
        mSharedPref.putSharePrefInteger(SharePrefConstant.color,color);
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
        if(mystyle!=null)
        {
            mystyle.setLine_width(width);
        }
        mSharedPref.putSharePrefInteger(SharePrefConstant.line_width,width);
    }

    public int getXuxian_width() {
        return xuxian_width;
    }

    public void setXuxian_width(int xuxian_width) {
        this.xuxian_width = xuxian_width;
        if(mystyle!=null)
        {
            mystyle.setXuxian_width(xuxian_width);
        }
        mSharedPref.putSharePrefInteger(SharePrefConstant.xuxian_width,xuxian_width);
    }

    public  void clipYuan(Canvas canvas)
    {

    }
}
